package com.project.javagobang.game;


import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

//房间管理器类,这个类也希望有唯一实例
@Component
public class RoomManager {
    private ConcurrentHashMap<String, Room> rooms = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer, String> userIdToRoomId = new ConcurrentHashMap<>();

    //添加
    public void add(Room room,int userId1,int userId2) {
        rooms.put(room.getRoomId(), room);
        userIdToRoomId.put(userId1,room.getRoomId());
        userIdToRoomId.put(userId2,room.getRoomId());
    }

    //删除
    public void remove(String roomId,int userId1,int userId2) {
        rooms.remove(roomId);
        userIdToRoomId.remove(userId1);
        userIdToRoomId.remove(userId2);
    }

    //查找roomid获取room
    public Room getRoomByRoomId(String roomId) {
        return rooms.get(roomId);
    }

    //查找userid获取room
    public Room getRoomByUserId(int userId) {
        String roomId = userIdToRoomId.get(userId);
        if (roomId == null) {
            //userid->roomid映射关系不存在,直接返回null
            return null;
        }
        return rooms.get(roomId);
    }
}
